﻿using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DotNet.Utilities
{
    public class SharpTargz
    {

        /// <summary>  
        /// 生成 ***.tar.gz 文件  
        /// </summary>  
        /// <param name="strBasePath">文件基目录（源文件、生成文件所在目录）</param>  
        /// <param name="strSourceFolderName">待压缩的源文件夹名</param>  
        public static bool CreatTarGzArchive(string strBasePath, string strSourceFolderName)
        {
            if (string.IsNullOrEmpty(strBasePath)
                || string.IsNullOrEmpty(strSourceFolderName)
                || !System.IO.Directory.Exists(strBasePath)
                || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))
            {
                return false;
            }

            Environment.CurrentDirectory = strBasePath;
            string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);
            string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar.gz");

            Stream outTmpStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);

            //注意此处源文件大小大于4096KB  
            Stream outStream = new GZipOutputStream(outTmpStream);
            TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);
            TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);
            archive.WriteEntry(entry, true);

            if (archive != null)
            {
                archive.Close();
            }

            outTmpStream.Close();
            outStream.Close();

            return true;
        }

        /// <summary>
        /// 文件解压
        /// </summary>
        /// <param name="zipPath">压缩文件路径</param>
        /// <param name="goalFolder">解压到的目录</param>
        /// <returns></returns>
        public static bool UnzipTgz(string zipPath, string goalFolder)
        {
            Stream inStream = null;
            Stream gzipStream = null;
            TarArchive tarArchive = null;
            try
            {
                using (inStream = File.OpenRead(zipPath))
                {
                    using (gzipStream = new GZipInputStream(inStream))
                    {
                        tarArchive = TarArchive.CreateInputTarArchive(gzipStream,Encoding.Default);
                        tarArchive.ExtractContents(goalFolder);
                        tarArchive.Close();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("压缩出错！"+ex.Message);
                return false;
            }
            finally
            {
                if (null != tarArchive) tarArchive.Close();
                if (null != gzipStream) gzipStream.Close();
                if (null != inStream) inStream.Close();
            }
        }
    }
}
